import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-05-07
 * Time: 22:51
 */
//生产者消费者模型
public class ThreadDemo17 {
    public static void main(String[] args) {
        BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<>();

        //消费者
        Thread t1 = new Thread(()->{
            while(true) {
                try {
                    int value = blockingDeque.take();
                    System.out.println("消费的元素："+value);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();

        //生产者
        Thread t2 = new Thread(()->{
            int value = 0;
           while(true) {
               try {
                   System.out.println("生产的元素："+value);
                   blockingDeque.put(value);
                   value++;
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });

        t2.start();
    }
}